import { requestGet } from "@/framework/util/http";
import path from "path-browserify-esm";
import type { ToolInstallVersionItem } from "@/pages/tools/common/types";

// interface DragonwellVersionResp {
//   version8: "8.20.21";
//   xurl8: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/8.20.21-test-dragonwell_standard/Alibaba_Dragonwell_Standard_8.20.21_x64_linux.tar.gz";
//   aurl8: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/8.20.21-test-dragonwell_standard/Alibaba_Dragonwell_Standard_8.20.21_aarch64_linux.tar.gz";
//   wurl8: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/8.20.21-test-dragonwell_standard/Alibaba_Dragonwell_Standard_8.20.21_x64_windows.zip";
//   type8: "oss";
//   version11: "11.0.24.20.8";
//   xurl11: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/11.0.24.20.8-test-dragonwell_standard/Alibaba_Dragonwell_Standard_11.0.24.20.8_x64_linux.tar.gz";
//   aurl11: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/11.0.24.20.8-test-dragonwell_standard/Alibaba_Dragonwell_Standard_11.0.24.20.8_aarch64_linux.tar.gz";
//   apurl11: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/11.0.24.20.8-test-dragonwell_standard/Alibaba_Dragonwell_Standard_11.0.24.20.8_x64_alpine-linux.tar.gz";
//   wurl11: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/11.0.24.20.8-test-dragonwell_standard/Alibaba_Dragonwell_Standard_11.0.24.20.8_x64_windows.zip";
//   rurl11: "";
//   type11: "oss";
//   version17: "17.0.12.0.13.7";
//   xurl17: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/17.0.12.0.13%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_17.0.12.0.13.7_x64_linux.tar.gz";
//   aurl17: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/17.0.12.0.13%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_17.0.12.0.13.7_aarch64_linux.tar.gz";
//   apurl17: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/17.0.12.0.13%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_17.0.12.0.13.7_x64_alpine-linux.tar.gz";
//   wurl17: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/17.0.12.0.13%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_17.0.12.0.13.7_x64_windows.zip";
//   type17: "oss";
//   version21: "21.0.4.0.4.7";
//   xurl21: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/21.0.4.0.4%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_21.0.4.0.4.7_x64_linux.tar.gz";
//   aurl21: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/21.0.4.0.4%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_21.0.4.0.4.7_aarch64_linux.tar.gz";
//   apurl21: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/21.0.4.0.4%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_21.0.4.0.4.7_x64_alpine-linux.tar.gz";
//   wurl21: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/21.0.4.0.4%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_21.0.4.0.4.7_x64_windows.zip";
//   type21: "oss";
// }

function getDragonwellVersions(versionInfo: any, edition: string): ToolInstallVersionItem[] {
  let keys = Object.keys(versionInfo);
  switch (proc.platform) {
    case "win32":
      keys = keys.filter(it => it.startsWith("wurl"));
      break;
    case "linux":
      keys = keys.filter(it => it.startsWith("xurl") || it.startsWith("apurl"));
      break;
  }
  return keys
    .filter(it => versionInfo[it])
    .map(it => {
      const url = versionInfo[it];
      const versionCode = it.replace(/^[a-zA-Z]+/, "");
      const version = versionInfo[`version${versionCode}`];
      return {
        id: `dragonwell-${edition}-${version}`,
        name: path.basename(url),
        version,
        edition,
        url,
      };
    });
}

export async function getDragonwellJdkDownloadList(): Promise<ToolInstallVersionItem[]> {
  try {
    const res = await requestGet("https://dragonwell-jdk.io/releases.json");
    const standardList = getDragonwellVersions(res.data.oss.standard, "standard");
    const extendedList = getDragonwellVersions(res.data.oss.extended, "extended");
    return [...standardList, ...extendedList];
  } catch (e: any) {
    console.error("getDragonwellJdkDownloadList error", e);
    return [];
  }
}

export interface LibericaJavaVersion {
  GA: boolean;
  LTS: boolean;
  featureVersion: number;
}

export async function getLibericaJavaVersions(): Promise<LibericaJavaVersion[]> {
  try {
    const resp = await requestGet("https://api.bell-sw.com/v1/liberica/versions");
    if (__DEV__) {
      console.log("getLibericaJavaVersions", resp.data);
    }
    return resp.data;
  } catch (e: any) {
    console.error("getLibericaJavaVersions error", e);
    return [];
  }
}

export async function getLibericaBundleTypes(): Promise<string[]> {
  try {
    const resp = await requestGet<string[]>("https://api.bell-sw.com/v1/liberica/bundle-types");
    if (__DEV__) {
      console.log("getLibericaBundleTypes", resp.data);
    }
    return resp.data;
  } catch (e: any) {
    console.error("getLibericaBundleTypes error", e);
    return [];
  }
}

// {
//   "bitness": 64,
//   "latestLTS": true,
//   "updateVersion": 1,
//   "downloadUrl": "https://github.com/bell-sw/Liberica/releases/download/21.0.1+12/bellsoft-jdk21.0.1+12-windows-aarch64-full.msi",
//   "latestInFeatureVersion": false,
//   "LTS": true,
//   "bundleType": "jdk-full",
//   "featureVersion": 21,
//   "packageType": "msi",
//   "FX": true,
//   "GA": true,
//   "architecture": "arm",
//   "latest": false,
//   "extraVersion": 0,
//   "buildVersion": 12,
//   "EOL": true,
//   "os": "windows",
//   "interimVersion": 0,
//   "version": "21.0.1+12",
//   "sha1": "46e76319568d559b1972ba7c6ccf8a763ccb73e2",
//   "filename": "bellsoft-jdk21.0.1+12-windows-aarch64-full.msi",
//   "installationType": "installer",
//   "size": 216875008,
//   "patchVersion": 0,
//   "TCK": true,
//   "updateType": "psu"
// }

export interface LibericaJdkVersionInfo {
  bitness: 64 | 32;
  latestLTS: boolean;
  updateVersion: number;
  downloadUrl: string;
  latestInFeatureVersion: boolean;
  LTS: boolean;
  bundleType: string;
  featureVersion: number;
  packageType: string;
  FX: boolean;
  GA: boolean;
  architecture: string;
  latest: boolean;
  extraVersion: number;
  buildVersion: number;
  EOL: boolean;
  os: string;
  interimVersion: number;
  version: string;
  sha1: string;
  filename: string;
  installationType: string;
  size: number;
  patchVersion: number;
  TCK: boolean;
  updateType: string;
}

export async function getLibericaJdkDownloadList(versionFeature: number, packageType: string): Promise<LibericaJdkVersionInfo[]> {
  const api = `https://api.bell-sw.com/v1/liberica/releases/?version-feature=${versionFeature}&os=${getLibericaOsName()}&arch=${getArch()}&bitness=${getBitness()}&bundle-type=${packageType}`;
  if (__DEV__) console.log("getLibericaJdkDownloadList", api);
  try {
    const resp = await requestGet<LibericaJdkVersionInfo[]>(api);
    if (__DEV__) console.log("getLibericaJdkDownloadList", resp.data);
    // 过滤出压缩包
    const list = resp.data.filter(it => it.installationType === "archive");
    list.sort((a, b) => b.updateVersion - a.updateVersion);
    return list;
  } catch (e: any) {
    console.error("getLibericaJdkDownloadList error", e);
    return [];
  }
}

function getLibericaOsName() {
  return proc.platform === "win32" ? "windows" : proc.platform;
}

function getArch() {
  switch (proc.arch) {
    case "arm":
    case "arm64":
      return "arm";

    case "ia32":
    case "x64":
      return "x86";

    case "ppc":
    case "ppc64":
      return "ppc";

    case "riscv64":
      return "riscv";
  }

  return "";
}

function getBitness() {
  return proc.arch === "ia32" || proc.arch === "arm" || proc.arch === "ppc" ? 32 : 64;
}

const packageTypeLabels: Dict<string> = {
  jdk: "Standard JDK",
  "jdk-crac": "CRaC JDK",
  "jdk-full": "Full JDK",
  "jdk-lite": "Lite JDK",
  "jdk-perf": "Performance JDK",
  jre: "Standard JRE",
  "jre-full": "Full JRE",
  "jre-perf": "Performance JRE",
};

export const getPackageTypeLabel = (type: string) => packageTypeLabels[type] || type;
